// objectMgr.cpp
//
// Objectging template object

#include "objectMgr.h"
#include "object.h"
#include "debugStream.h"

#include <iostream>

namespace simple{
    //
    // ObjectMgr
    //
    ObjectMgr* ObjectMgr::_sOnly = 0;

    ObjectMgr& ObjectMgr::gOnly ()
    {
        if (!_sOnly)
            _sOnly = new ObjectMgr;

        return *_sOnly;
    }


    ObjectMgr::ObjectMgr ()
    {}


    void ObjectMgr::add (const std::string& name, ObjectInfoBase* obj)
    {
        _mapping[name] = obj;
    }

    ObjectInfoBase* ObjectMgr::find (const std::string& name)
    {
        OBJMAP::iterator i = _mapping.find (name);
        if (i == _mapping.end())
            return 0;
        return i->second;
    }


    void ObjectMgr::debugMessage (const std::string& header,
                                                                    const std::string& msg)
    {
        cdebug << header.c_str() << msg.c_str() << std::endl;
    }


    std::string ObjectMgr::dump ()
    {
        std::string str;
        char buffer[16];

        OBJMAP::iterator i;
        for (i=_mapping.begin(); i != _mapping.end(); ++i) {
            sprintf (buffer, " %d:    ", i->second->debug());
            str += i->first + buffer;
            str += i->second->dump ();
            str += "\n";
        }

        return str;
    }

    std::string ObjectMgr::process (const std::string& command)
    {
        std::string result;
        OBJMAP::iterator i;
        for (i=_mapping.begin(); i != _mapping.end(); ++i) {
            result += i->second->process (command);
            result += " ";
        }
        return result;
    }
}